#include "buzz_app.h"
#include "buzz_board.h"

#if _VOICE_PLAY_TYPE_ == _VOICE_PLAY_BUZZ_
/**
 * @brief 蜂鸣器音频播放回调
 *
 */
static void BuzzPlayVolceCallback(void);

static BuzzStatusBit_t BuzzStatusBit = {0};

static void BuzzPlayVolceCallback(void)
{
  uint32_t NextWakeupTime = 0;

  switch (BuzzStatusBit.ExecuteStatus)
  {
  case BUZZ_HARDWARE_ALARM:
  {
    switch (BuzzStatusBit.HintMode)
    {
    case BUZZ_PLAY_REGISTER_SUCCESS:
      NextWakeupTime = ((BuzzStatusBit.HintCount % BUZZ_PLAY_REGISTER_SUCCESS_COUNT) == 0) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_WEAK_CUE_FLASHING_PERIOD;
      break;

    case BUZZ_PLAY_SET_SUCCESS:
      NextWakeupTime = ((BuzzStatusBit.HintCount % BUZZ_PLAY_SET_SUCCESS_COUNT) == 0) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_WEAK_CUE_FLASHING_PERIOD;
      break;

    case BUZZ_PLAY_SYSTEM_POWERUP:
      NextWakeupTime = ((BuzzStatusBit.HintCount) == 1) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_WEAK_CUE_FLASHING_PERIOD;
      break;

    case BUZZ_PLAY_TIME_ERROR:
    case BUZZ_PLAY_LOCAL_REGISTER:
      NextWakeupTime = BUZZ_STRONG_PROMPTING_FLASHING_PERIOD;
      break;

    case BUZZ_PLAY_SYSTEM_LOCKED:
      NextWakeupTime = BUZZ_LONG_DELAY_TIME;
      break;

    case BUZZ_PLAY_SELF_TEST_ERROR:
      NextWakeupTime = ((BuzzStatusBit.HintCount) == 1) ? BUZZ_LONG_DELAY_TIME : BUZZ_WEAK_CUE_FLASHING_PERIOD;
      break;

    default:
      NextWakeupTime = BUZZ_WEAK_CUE_FLASHING_PERIOD;
      break;
    }

    SetBuzzPinLevel(true);
    BuzzSetBusyStatus(true);
    BuzzDelayedStartPlaySoundTask(NextWakeupTime);
    BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_STOP;

    if (BuzzStatusBit.HintCount > 0)
    {
      BuzzStatusBit.HintCount--;
    }
  }
  break;

  case BUZZ_HARDWARE_STOP:
  {
    SetBuzzPinLevel(false);

    if (BuzzStatusBit.HintCount > 0)
    {
      BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

      switch (BuzzStatusBit.HintMode)
      {
      case BUZZ_PLAY_SYSTEM_INITIALIZE:
        NextWakeupTime = ((BuzzStatusBit.HintCount % 4) == 0) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_MEDIUM_FLASHING_PERIOD;
        break;

      case BUZZ_PLAY_SET_SUCCESS:
        NextWakeupTime = ((BuzzStatusBit.HintCount % 3) == 2) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_MEDIUM_FLASHING_PERIOD;
        break;

      case BUZZ_PLAY_SYSTEM_POWERUP:
        NextWakeupTime = ((BuzzStatusBit.HintCount % 3) == 2) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_MEDIUM_FLASHING_PERIOD;
        break;

      case BUZZ_PLAY_FALSE_LOCK:
        NextWakeupTime = ((BuzzStatusBit.HintCount % 3) == 0) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_MEDIUM_FLASHING_PERIOD;
        break;

      case BUZZ_PLAY_LOW_POWER:
        NextWakeupTime = ((BuzzStatusBit.HintCount % 2) == 0) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_MEDIUM_FLASHING_PERIOD;
        break;

      case BUZZ_PLAY_CONNECT_TEST:
        NextWakeupTime = ((BuzzStatusBit.HintCount % 5) == 0) ? BUZZ_STRONG_PROMPTING_FLASHING_PERIOD : BUZZ_MEDIUM_FLASHING_PERIOD;
        break;

      default:
        NextWakeupTime = BUZZ_MEDIUM_FLASHING_PERIOD;
        break;
      }

      BuzzDelayedStartPlaySoundTask(NextWakeupTime);
    }
    else
    {
      BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_IDLE;
      BuzzStartPlaySoundTaskNoWait();
    }
  }
  break;

  default:
  {
    BuzzSetBusyStatus(false);
  }
  break;
  }
}

void BuzzPlayDeviceLockedAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_LOCKED;
  BuzzStatusBit.HintCount = BUZZ_PLAY_LOCKED_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayKeyToneAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_KEY_TONE;
  BuzzStatusBit.HintCount = BUZZ_PLAY_KEY_TONE_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayLowPowerAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_LOW_POWER;
  BuzzStatusBit.HintCount = BUZZ_PLAY_LOW_POWER_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySetParameterFailureAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SET_PARAMETER_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SET_ERROR_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayTimeErrorAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_TIME_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_TIME_ERROR_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayDeviceBusyAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_DEVICE_BUSY;
  BuzzStatusBit.HintCount = BUZZ_PLAY_DEVICE_BUSY_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayFalseLockAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_FALSE_LOCK;
  BuzzStatusBit.HintCount = BUZZ_PLAY_FALSE_LOCK_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySetSuccessAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SET_SUCCESS;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SET_SUCCESS_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayDoorActionAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_DOOR_UNLOCK;
  BuzzStatusBit.HintCount = BUZZ_PLAY_DOOR_UNLOCK_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayKeyRemoveAudio(void)
{
  BuzzStatusBit.HintCount = 0;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_STOP;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayKeyInsertAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_KEY_INSERTED;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;
  BuzzStatusBit.HintCount = BUZZ_PLAY_KEY_INSERTED_COUNT;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayCardReplaceAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_CARD_REPLACE;
  BuzzStatusBit.HintCount = BUZZ_PLAY_CARD_REPLACE_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayConnectTestAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_CONNECT_TEST;
  BuzzStatusBit.HintCount = BUZZ_PLAY_CONNECT_TEST_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySystemLockedAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SYSTEM_LOCKED;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SYSTEM_LOCKED_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayLocalRegisterAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_LOCAL_REGISTER;
  BuzzStatusBit.HintCount = BUZZ_PLAY_LOCAL_REGISTER_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayRegisterErrorAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_REGISTER_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_REGISTER_ERROR_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySystemPowerupAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SYSTEM_POWERUP;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SYSTEM_POWERUP_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySelfTestErrorAudio(uint8_t ErrorCode)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SELF_TEST_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SELF_TEST_ERROR_COUNT + ErrorCode;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayDeviceActivatedAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_DEVICE_ACTIVATED;
  BuzzStatusBit.HintCount = BUZZ_PLAY_DEVICE_ACTIVATED_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayRegisterSuccessAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_REGISTER_SUCCESS;
  BuzzStatusBit.HintCount = BUZZ_PLAY_REGISTER_SUCCESS_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySystemInitializeAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SYSTEM_INITIALIZE;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SYSTEM_INITIALIZE_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayCardReportedLostAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_CARD_REPORTED_LOST;
  BuzzStatusBit.HintCount = BUZZ_PLAY_CARD_REPORTED_LOST_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlaySetParameterErrorAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_SET_PARAMETER_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_SET_ERROR_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayAntiBreakingAlarmAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_ANTI_BREAKING_ALARM;
  BuzzStatusBit.HintCount = BUZZ_PLAY_ANTI_BREAKING_ALARM_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayExpiresLicenseCodeAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_EXPIRES_LICENSE_CODE;
  BuzzStatusBit.HintCount = BUZZ_PLAY_EXPIRES_LICENSE_CODE_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayCardAffiliationErrorAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_CAED_AFFILIATION_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_CAED_AFFILIATION_ERROR_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayThisFunctionCardHasBeenDisabledAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_FUNCTION_CARD_DISABLED;
  BuzzStatusBit.HintCount = BUZZ_PLAY_FUNCTION_CARD_DISABLED_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayInsufficientPrivilegesAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_INSUFFICIENT_PRIVILEGES;
  BuzzStatusBit.HintCount = BUZZ_PLAY_INSUFFICIENT_PRIVILEGES_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzPlayNetworkAttachmentErrorAudio(void)
{
  BuzzStatusBit.HintMode = BUZZ_PLAY_NETWORK_ATTACHMENT_ERROR;
  BuzzStatusBit.HintCount = BUZZ_PLAY_NETWORK_ATTACHMENT_ERROR_COUNT;
  BuzzStatusBit.ExecuteStatus = BUZZ_HARDWARE_ALARM;

  BuzzStartPlaySoundTaskNoWait();
}

void BuzzConfiguration(void)
{
  BuzzExternalInterfaces_t ExternalInterfaces = {0};
  ExternalInterfaces.SoundEventTask = BuzzPlayVolceCallback;

  BuzzBoardInitialisation(&ExternalInterfaces);
}

#endif
